package app.fugema.shop.main.center.view;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DialogUtils;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.connector.OnItemClickInterface;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityCollectViewBinding;
import app.fugema.shop.main.center.adapter.CollectShopAdapter;
import app.fugema.shop.main.center.model.CollectShopResult;
import app.fugema.shop.main.center.vm.UserVM;
import app.fugema.shop.main.goods.view.ShopActivity;


public class CollectShopActivity extends BaseTitleActivity<ActivityCollectViewBinding, UserVM> implements OnGetDataListener
{
    private String userKey;

    private boolean isEdit = false;

    public static void start(Context context)
    {
        Intent intent = new Intent(context, CollectShopActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_view);
    }

    private CollectShopAdapter adapter;

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("收藏的店铺",R.color.white);
        setRightText("编辑",R.color.white);
        adapter = new CollectShopAdapter();
        binding.rvRecyclerView.setAdapter(adapter);

        userKey = LoginUtils.getToken();

        viewUtils.setRefreshStateLayout(binding.rvRecyclerView,this,this);
        //编辑
        setRightTextOnClick(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                buttonTextChanged();
            }
        });

        adapter.setOnItemClickInterface(new OnItemClickInterface() {
            @Override
            public boolean onItemClick(View view, int onclickType, int itemType, int position, Object bean) {

                if(bean instanceof CollectShopResult.CollectShop)
                {
                    switch ( view.getId() )
                    {
                        case R.id.btn_delete:
                            DialogUtils.showSimpleDialog(getActivity(), false, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    dialog.dismiss();
                                    cancelShopById(((CollectShopResult.CollectShop)bean).getShopId());
                                }
                            }, "确认取消收藏？");
                            break;
                        default:
                            ShopActivity.start(view.getContext(), ((CollectShopResult.CollectShop)bean).getShopId());
                            break;
                    }
                }
                return false;
            }
        });


    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        onGetData(1);
    }

    @Override
    public void onGetData(int pn)
    {
        viewModel.getCollectionShop(pn,userKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<CollectShopResult>>(getActivity())
                 {
                     @Override
                     protected void onSuccess(LiveDataWrapper<CollectShopResult> data)
                     {
                         viewUtils.setDataListRefreshLayout(adapter,pn,10,data.data.getData());
                     }

                     @Override
                     public boolean isEmptyData(LiveDataWrapper<CollectShopResult> data)
                     {
                         return pn==1 && UIUtils.isEmpty(data.data);
                     }
                 });
    }

    private void buttonTextChanged()
    {
        if (isEdit)
        {
            isEdit = false;
            setRightText("管理");
            adapter.setEdit( isEdit );
        }
        else
        {
            setRightText("保存");
            isEdit = true;
            adapter.setEdit( isEdit );
        }
    }

    private void cancelShopById(long shopId)
    {
        viewModel.cancelCollectShop(shopId,userKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>() {
                     @Override
                     protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                     {
                         //{"success":true,
                         //    "msg":"取消成功！"}
                        if( data.data.isSuccess() )
                        {
                            UIUtils.showToastSafes(data.data.getMsg());
                            onGetData(1);
                        }
                     }
                 });
    }
}
